package com.zxc.controller.Crops;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxc.model.Apply.ApplyWarehouse;
import com.zxc.model.Crops.CropsVO;
import com.zxc.model.Page;
import com.zxc.model.Trading.TradingVO;
import com.zxc.service.CropsService;
import com.zxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
@RequestMapping("/Crops")
public class CropsController {
    @Autowired
    private CropsService cropsService;

    @Autowired
    private UserService userService;


    @RequestMapping("/getDate")
    public ResponseEntity<?> getDate(@RequestBody CropsVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        String id =userService.getUser(page.getAccount()).getId();
        vo.setUserId(id);
        PageHelper.startPage(page.getOffset(), page.getLimit());
        response.put("CropsList",new PageInfo<CropsVO>(cropsService.getCrocsList(vo)));
        return ResponseEntity.ok(response);
    }

    @RequestMapping("/insertDate")
    public ResponseEntity<?> insertDate(@RequestBody TradingVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        cropsService.setTrading(vo);
        return ResponseEntity.ok(response);
    }
//202003031751 园区售出插入语句。
    @RequestMapping("/insertDateByPark")
    public ResponseEntity<?> insertDateByPark(@RequestBody TradingVO vo) {
        return ResponseEntity.ok(cropsService.setTradingPark(vo));
    }


    @RequestMapping("/getRecordDate")
    public ResponseEntity<?> getRecordDate(@RequestBody TradingVO vo,@RequestParam int RecordId) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        if("root".equals(page.getAccount())){

        }else {
            String id =userService.getUser(page.getAccount()).getId();
            vo.setUserId(id);
        }
        PageHelper.startPage(page.getOffset(), page.getLimit());
        if (RecordId == 1) {
            response.put("RecordList",new PageInfo<TradingVO>(cropsService.getRecordList(vo)));
        }else {
            response.put("RecordList",new PageInfo<TradingVO>(cropsService.getRecordListNo(vo)));
        }
        return ResponseEntity.ok(response);
    }


    @RequestMapping("/getDetails")
    public ResponseEntity<?> getDetails(@RequestBody CropsVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("CropsList",cropsService.getCrocsList(vo));
        return ResponseEntity.ok(response);
    }

    //按月份展示2020年销售金额
    @RequestMapping("/getMonthMoney")
    public ResponseEntity<?> getMonthMoney(@RequestBody TradingVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("MonthMoneyList",cropsService.getMonthMoneyByUserId(vo));
        return ResponseEntity.ok(response);
    }

//仓内作物
    //审批操作
    @RequestMapping("/changeTradingApplyStateOk")
    public ResponseEntity<?> changeTradingApplyStateOk(@RequestBody TradingVO tradingVO) {
//        审批通过
        return ResponseEntity.ok(cropsService.changeApplyTradingStateOk(tradingVO));
    }
    @RequestMapping("/changeTradingApplyStateNo")
    public ResponseEntity<?> changeTradingApplyStateNo(@RequestBody TradingVO tradingVO) {
//        审批不通过
        return ResponseEntity.ok(cropsService.changeApplyTradingStateNo(tradingVO));
    }


    //作物入仓
    @RequestMapping("/insertCropsIntoWarehouse")
    public ResponseEntity<?> insertCropsIntoWarehouse(@RequestBody CropsVO cropsVO) {
        return ResponseEntity.ok(cropsService.setCropsIntoWarehouse(cropsVO));
    }


}
